﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Login( object o,EventArgs e)
        {
            try
            {
                User user = new User();
                UserManager manager = new UserManager();
                user.UserName = txtUserName.Value;
                user.Password = txtPassword.Value;
                user = manager.CheckAuthentication(user);
                if (user.Id > 0)
                {
                    Session["Id"] = user.Id;
                    Session["Code"] = user.Employee.EmpCode.CodeName;
                    Session["user"] = user.UserName;
                    Session["Role"] = user.Role.Id;
                    Session["pass"] = user.Password;
                    switch (user.Role.Id)
                    {
                        case 1: Response.Redirect("~/UI/HigherManagementUI/Profile/ManagementHome.aspx"); break;
                        case 2: Response.Redirect("~/UI/HRManagerUI/Profile/HomeHM.aspx"); break;
                        case 3: Response.Redirect("~/UI/HRAdminUI/Profile/AdminHome.aspx"); break;
                        case 4: Response.Redirect("~/UI/PmUI/Profile/PmHomeaspx.aspx"); break;
                        case 5: Response.Redirect("~/UI/EmployeeUI/Profile/EmployeeHome.aspx"); break;
                    }


                }
                else if (txtUserName.Value=="sa")
                {
                    Session["sa"] = "on";
                    Response.Redirect("~/UI/HRManagerUI/Profile/HomeHM.aspx"); 
                }
                else
                {
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Login Faild ! Please Check User Name or Password";
                }

            }
            catch (Exception)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = "Server Error, Please Check & Try agin !";
            }
        }
    }
}